package ChatNI;

import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
import ourMessage.*;

/**
 *
 * @author Ridiss
 */
public class FileSender extends Thread{
        private int port;
        private int compteurpart = 0;
        private ServerSocket servSock;
        private Socket clientSocket;
        private File fichier;
        private FileInputStream file;
        private BufferedWriter writer;
        private InputStream reader;
        private OutputStreamWriter out;
        private byte[] buffer;
        private long fsize;
        
    /**
     *
     * @param portEnvoi
     * @param file
     * @param size
     * @throws IOException
     */
    public FileSender(int portEnvoi, File file,long size) throws IOException {
                // TODO Auto-generated constructor stub
            System.out.println("port et file "+portEnvoi+" "+file.toString());
                this.port = portEnvoi;
                this.fichier=file;
                this.fsize=size;
                 this.buffer = new byte[(int)fsize];
               start();
                                
        }

        @Override
        public void run() {
                // TODO Auto-generated method stub  
                        while(true){
                            
                            try {
                                this.servSock = new ServerSocket(port);
                                  clientSocket = servSock.accept();
                                  System.out.println("client sock"+ clientSocket);
                                  out = new OutputStreamWriter(clientSocket.getOutputStream());
                                  writer = new BufferedWriter(out);
                                  reader = clientSocket.getInputStream();
                                  SendFilePart();  
                                 servSock.close();
                                 clientSocket.close();
                                 servSock=null;
                                 clientSocket=null;
                                 file.close();
                 System.out.println("il s'arret");
                                 
                            } catch (IOException ex) {
                                Logger.getLogger(FileSender.class.getName()).log(Level.SEVERE, null, ex);
                            }
                
                        }
                       
                
                
        }
        
    /**
     *
     * @throws IOException
     */
    public void SendFilePart() throws IOException{
               
                DataOutputStream outToClient = new DataOutputStream(clientSocket.getOutputStream());
                file= new FileInputStream(fichier);
                buffer=new byte[20*4096];
                int c;
               while((c=file.read(buffer))!=-1){

                    outToClient.write(buffer,0,c);
                    outToClient.flush();
                }
                outToClient.close();
                file.close();
                clientSocket.close();

            System.out.println("on vient de finir ");
         
        }

}